<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // // s = "loveleetcode";
        // s = "aaleetcode";
        // function fn(s) {
        //     const arr = s.split('')
        //     const a = []
        //     for (let i = 0; i < arr.length; i++) {
        //         for (let j = arr.length-1; j > i; j--) {
        //             if ( arr[i] === arr[j]) {
        //                 a.push(arr[i])
        //             } 
        //         }
        //         if(!a.includes(arr[i])){
        //                 return i
        //             }
        //     }
        //     return -1
        // }

        // console.log(fn(s));
        // s = "loveleetcode";


            function fn2 (s){
                for(let i = 0; i < s.length ; i++){
                    if(s.indexOf(s[i]) === s.lastIndexOf(s[i]) ){
                        return i
                    }
                }
            }

        s = "xbabx";
        function fn(s) {
            const arr = s.split('');
            const obj = {};
            for (const item of arr) {
                if (!obj[item]) {
                    obj[item] = 1
                } else {
                    obj[item]++
                }
            }
            let str;    
            for (const k in obj) {
                if (obj[k] === 1) {
                    str = obj[k]
                }
            }

            for (let i = 0; i < arr.length; i++) {
                if (arr[i] === str) {
                    return i
                }
            }
            return -1
        }

        console.log(fn(s));
        console.log(fn2(s));
    </script>
</body>

</html>